﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Common;
using PM.Entity;
using System.Collections.Generic;

namespace PM.Site.Admin
{
    public partial class SecurityOverview : System.Web.UI.Page
    {
        private SecurityCollection originalCollection;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }
            }
            else
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                originalCollection = SecurityServices.GetSecurityCollection();
                Session["SecurityCollection"] = originalCollection;
                dlSecurity.DataSource = originalCollection;
                dlSecurity.DataBind();
            }
            
        }

        protected void dlSecurity_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string securityId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "EditSecurity":
                    Session["SecurityId"] = securityId;
                    Response.Redirect("~/Admin/SecurityForm.aspx");
                    break;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            List<Security> originalSecurityList = ((SecurityCollection)Session["SecurityCollection"]).ToList<Security>();
            SecurityCollection updatedList = new SecurityCollection();
            bool update = false;
            

            foreach (DataListItem item in dlSecurity.Items)
            {
                double marketValue = 0;
                string securityId = dlSecurity.DataKeys[item.ItemIndex].ToString();
                Security security = new Security(securityId);
                Predicate<Security> findMatch = new Predicate<Security>(security.MatchFound);
                security = originalSecurityList.Find(findMatch);
                TextBox txbMktValue = (TextBox)item.FindControl("txbMktValue");
                if (Double.TryParse(txbMktValue.Text, out marketValue))
                {
                    if (security.MarketValue != marketValue)
                    {
                        security.MarketValue = marketValue;
                        security.LastModDate = DateTime.Today;
                        updatedList.Add(security);
                        update = true;
                    }
                }

                if (update)
                {
                    SecurityServices.UpdateCollection(updatedList);
                    Response.Redirect("~/Admin/SecurityOverview.aspx");
                    Session.Remove("SecurityCollection");
                }

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session["SecurityId"] = null;
            Response.Redirect("~/Admin/SecurityForm.aspx");
        }
    }
}
